//
//  YNBroadcastNotificationChat.m
//  Messenger
//
//  Created by iOS on 15/6/15.
//  Copyright (c) 2015年 YN-APP-iOS. All rights reserved.
//

#import "YNBroadcastNotificationChat.h"
#import "YNIMFrameworkMacro.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FMDatabaseAdditions.h"
#import "YNIMTool.h"

@implementation YNBroadcastNotificationChat

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if (!value){
        if ([key isEqualToString:@"broadcastNotificationType"]){
            self.broadcastNotificationType=0;
        }else if ([key isEqualToString:@"sourceTerminal"]){
            self.sourceTerminal=0;
        }else if ([key isEqualToString:@"isRead"]){
            self.isRead=0;
        }
    }
    
}

+ (NSString *)packetId
{
    return @"packetId";
}

+ (NSString *)subject
{
    return @"subject";
}

+ (NSString *)fromUserNo
{
    return @"fromUserNo";
}

+ (NSString *)toUserNo
{
    return @"toUserNo";
}

+ (NSString *)content
{
    return @"content";
}

+ (NSString *)link
{
    return @"link";
}

+ (NSString *)mTime
{
    return @"mTime";
}

+ (NSString *)senderName
{
    return @"senderName";
}

+ (NSString *)recipient
{
    return @"recipient";
}

+ (NSString *)broadcastNotificationType
{
    return @"broadcastNotificationType";
}

+ (NSString *)sourceTerminal
{
    return @"sourceTerminal";
}

+ (NSString *)isRead
{
    return @"isRead";
}

+ (NSString *)tableName
{
    return @"BroadcastNotificationChat";
}

- (NSArray *)insertParam
{
    return @[[YNIMTool nilFilter:self.packetId],
             [YNIMTool nilFilter:self.subject],
             [YNIMTool nilFilter:self.fromUserNo],
             [YNIMTool nilFilter:self.toUserNo],
             [YNIMTool nilFilter:self.content],
             [YNIMTool nilFilter:self.link],
             [YNIMTool nilFilter:self.mTime],
             [YNIMTool nilFilter:self.senderName],
             [YNIMTool nilFilter:self.recipient],
             [NSNumber numberWithInt:self.broadcastNotificationType],
             [NSNumber numberWithInt:self.sourceTerminal],
             [NSNumber numberWithInt:self.isRead]];
}

+ (BOOL)creatBroadcastNotificationTableInDatabase:(FMDatabase *)db
{
    NSString *creatBroadcastNotificationSql = [NSString  stringWithFormat:@"%@%@(%@ varchar(30) NOT NULL PRIMARY KEY, %@ varchar(30), %@ varchar(30), %@ varchar(30), %@ varchar(100), %@ varchar(100), %@ varchar(30), %@ varchar(30), %@ varchar(100), %@ INTEGER, %@ INTEGER, %@ INTEGER)",
                                               CREATE_TABLE,
                                               [YNBroadcastNotificationChat tableName],
                                               [YNBroadcastNotificationChat packetId],
                                               [YNBroadcastNotificationChat subject],
                                               [YNBroadcastNotificationChat fromUserNo],
                                               [YNBroadcastNotificationChat toUserNo],
                                               [YNBroadcastNotificationChat content],
                                               [YNBroadcastNotificationChat link],
                                               [YNBroadcastNotificationChat mTime],
                                               [YNBroadcastNotificationChat senderName],
                                               [YNBroadcastNotificationChat recipient],
                                               [YNBroadcastNotificationChat broadcastNotificationType],
                                               [YNBroadcastNotificationChat sourceTerminal],
                                               [YNBroadcastNotificationChat isRead]];
    BOOL result = [db executeUpdate:creatBroadcastNotificationSql];
    return result;
}

- (BOOL)insertBroadcastNotificationInDatabase:(FMDatabase *)db
{
    NSString *filedString = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@",
                             [YNBroadcastNotificationChat packetId],
                             [YNBroadcastNotificationChat subject],
                             [YNBroadcastNotificationChat fromUserNo],
                             [YNBroadcastNotificationChat toUserNo],
                             [YNBroadcastNotificationChat content],
                             [YNBroadcastNotificationChat link],
                             [YNBroadcastNotificationChat mTime],
                             [YNBroadcastNotificationChat senderName],
                             [YNBroadcastNotificationChat recipient],
                             [YNBroadcastNotificationChat broadcastNotificationType],
                             [YNBroadcastNotificationChat sourceTerminal],
                             [YNBroadcastNotificationChat isRead]];
    
    NSString *insertBroadcastNotificationSql = [NSString stringWithFormat:@"%@%@ (%@) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", INSERT_TABLE, [YNBroadcastNotificationChat tableName], filedString];
    BOOL result = [db executeUpdate:insertBroadcastNotificationSql withArgumentsInArray:[self insertParam]];
    return result;
}

+ (BOOL)deleteBroadcastWithNotificationTypeArray:(NSArray *)typeArray inDatabase:(FMDatabase *)db
{

    NSString *deleteManyBroadcastNotificationSql = [NSString stringWithFormat:@"DELETE FROM %@ ", [YNBroadcastNotificationChat tableName]];
    
    for (int sum = 0; sum < typeArray.count; sum ++)
    {
        NSNumber *number = [typeArray objectAtIndex:sum];
        if (sum == 0)
        {
             deleteManyBroadcastNotificationSql = [NSString stringWithFormat:@"%@ WHERE %@ = %@ ", deleteManyBroadcastNotificationSql, [YNBroadcastNotificationChat broadcastNotificationType], number];
        }
        else
        {
            deleteManyBroadcastNotificationSql = [NSString stringWithFormat:@"%@ OR %@ = %@", deleteManyBroadcastNotificationSql, [YNBroadcastNotificationChat broadcastNotificationType], number];
        }
    }
    
    BOOL result = [db executeUpdate:deleteManyBroadcastNotificationSql];
    return result;
}

+ (BOOL)deleteBroadcastNotificationWithPacketId:(NSString *)packetId inDatabase:(FMDatabase *)db
{
    NSString *deleBroadcastNotificationSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", [YNBroadcastNotificationChat tableName], [YNBroadcastNotificationChat packetId]];
    BOOL result = [db executeUpdate:deleBroadcastNotificationSql, packetId];
    return result;
}

//更新未读通知为已读
+ (BOOL)updateBroadcastNotificationWithType:(int)broadcastNotificationType inDatabase:(FMDatabase *)db
{
    if (broadcastNotificationType == 3)//全部数据更新为已读
    {
        NSString *updateBroadcastNotiSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '1' WHERE %@ = '0'",[YNBroadcastNotificationChat tableName],[YNBroadcastNotificationChat isRead], [YNBroadcastNotificationChat isRead]];
        
        BOOL result = [db executeUpdate:updateBroadcastNotiSql];
        return result;
    }
    else
    {
    
        NSString *updateBroadcastNotiSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '1' WHERE %@ = %@ AND %@ = '0'",[YNBroadcastNotificationChat tableName],[YNBroadcastNotificationChat isRead],
                                            [YNBroadcastNotificationChat broadcastNotificationType],
                                            [NSNumber numberWithBool:broadcastNotificationType],[YNBroadcastNotificationChat isRead]];
        
        BOOL result = [db executeUpdate:updateBroadcastNotiSql];
        return result;
    
    }
    
}

//更新一条未读通知为已读
+ (BOOL)updateOneBroadcastNotificationWithPacketId:(NSString *)packetId inDatabase:(FMDatabase *)db
{
    
    NSString *updateBroadcastNotiSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '1' WHERE %@ = ?",[YNBroadcastNotificationChat tableName],[YNBroadcastNotificationChat isRead],
                                        [YNBroadcastNotificationChat packetId]];
    
    BOOL result = [db executeUpdate:updateBroadcastNotiSql,packetId];
    return result;
}



//根据类型查询通知
+ (NSArray *)queryBroadcastNotificationWithType:(int)broadcastNotificationType inDatabase:(FMDatabase *)db
{
    NSString *orderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC", [YNBroadcastNotificationChat mTime]];
    NSString *broadcastNotificationSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = %@ %@",
                                          [YNBroadcastNotificationChat tableName],
                                          [YNBroadcastNotificationChat broadcastNotificationType],
                                          [NSNumber numberWithBool:broadcastNotificationType],
                                          orderByStr];
    NSMutableArray *broadcastArray = [NSMutableArray array];
    FMResultSet *rs = [db executeQuery:broadcastNotificationSql];
    
    while ([rs next])
    {
        YNBroadcastNotificationChat *broadcast = [[YNBroadcastNotificationChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [broadcast setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [broadcastArray addObject:broadcast];
    }
    [rs close];
    return broadcastArray;
}

//查询未读通知
+ (NSArray *)queryUnReadBroadcastNotificationWithType:(int)broadcastNotificationType inDatabase:(FMDatabase *)db
{
    NSString *orderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC", [YNBroadcastNotificationChat mTime]];
    
    NSString *broadcastNotificationSql;
    
    if (broadcastNotificationType == 3)//
    {
        broadcastNotificationSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ? %@",
                                    [YNBroadcastNotificationChat tableName],
                                    [YNBroadcastNotificationChat isRead],
                                    orderByStr];
    }
    else
    {
        broadcastNotificationSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = %@ AND %@ = ? %@",
                                    [YNBroadcastNotificationChat tableName],
                                    [YNBroadcastNotificationChat broadcastNotificationType],
                                    [NSNumber numberWithBool:broadcastNotificationType],
                                    [YNBroadcastNotificationChat isRead],
                                    orderByStr];
        
    }
    
    NSMutableArray *broadcastArray = [NSMutableArray array];
    FMResultSet *rs = [db executeQuery:broadcastNotificationSql,@"0"];
    
    while ([rs next])
    {
        YNBroadcastNotificationChat *broadcast = [[YNBroadcastNotificationChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [broadcast setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [broadcastArray addObject:broadcast];
    }
    [rs close];
    return broadcastArray;
}


@end
